//
//  YinhangViewController.m
//  盘盈贷
//
//  Created by iOS on 15/3/26.
//  Copyright (c) 2015年 Yunpeng Technologies. All rights reserved.
//

#import "YinhangViewController.h"
#import "NIDropDown.h"
#import "QuartzCore/QuartzCore.h"
#import "LMContainsLMComboxScrollView.h"
#import "LMComBoxView.h"

#define kDropDownListTag 1000

@interface YinhangViewController ()<NIDropDownDelegate, LMComBoxViewDelegate>
{
    UIButton *select;
    NIDropDown *dropDown;
    UITableView *tv;//下拉列表
    NSMutableArray *tableArray;//下拉列表数据
    UITextField *textField;//文本输入框
    BOOL showList;//是否弹出下拉列表
    CGFloat tabheight;//table下拉列表的高度
    CGFloat frameHeight;//frame的高度
    
    
    LMContainsLMComboxScrollView *bgScrollView;
    NSMutableDictionary *addressDict;   //地址选择字典
    NSDictionary *areaDic;
    NSArray *province;
    NSArray *city;
    NSArray *district;
    
    NSString *selectedProvince;
    NSString *selectedCity;
    NSString *selectedArea;
}


@end

@implementation YinhangViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"银行卡绑定";
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGFloat height = self.view.frame.size.height;
    CGFloat width = self.view.frame.size.width;
    UILabel *top = [[UILabel alloc] initWithFrame:CGRectMake(5, height / 8.1, 100, 20)];
    top.text = @"选择银行 :";
    top.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:top];
    [top release];
    select = [[UIButton alloc] initWithFrame:CGRectMake(10, height / 5.6, width / 1.2, 30)];
    select.layer.borderWidth = 1;
    select.layer.borderColor = [[UIColor blackColor] CGColor];
    select.layer.cornerRadius = 5;
    [select addTarget:self action:@selector(selectClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:select];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(5, height / 4.3, 200, 20)];
    name.text = @"您的银行账户主姓名:";
    name.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:name];
    [name release];
    
    UILabel *kahao = [[UILabel alloc] initWithFrame:CGRectMake(5, height / 3.7, 100, 20)];
    kahao.text = @"银行卡号";
    kahao.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:kahao];
    [kahao release];
    
    
    UITextField *yinhao = [[UITextField alloc] initWithFrame:CGRectMake(10, height / 3.2, width / 1.2, 30)];
    yinhao.placeholder = @"银行卡号";
    [yinhao.layer setCornerRadius:4];
    [yinhao.layer setBorderWidth:1];
    yinhao.textColor = [UIColor grayColor];
    yinhao.borderStyle = UITextBorderStyleRoundedRect;
    yinhao.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:yinhao];
    [yinhao release];
    
    UILabel *city1 = [[UILabel alloc] initWithFrame:CGRectMake(5, height / 2.65, 100, 20)];
    city1.text = @"开户城市";
    city1.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:city1];
    [city1 release];
    //解析全国省市区信息
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
    
    areaDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    NSArray *components = [areaDic allKeys];
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSLog(@"%@",  plistPath);
    NSArray *sortedArray = [components sortedArrayUsingComparator: ^(id obj1, id obj2) {
        
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    
    NSMutableArray *provinceTmp = [NSMutableArray array];
    for (int i=0; i<[sortedArray count]; i++) {
        NSString *index = [sortedArray objectAtIndex:i];
        NSArray *tmp = [[areaDic objectForKey: index] allKeys];
        [provinceTmp addObject: [tmp objectAtIndex:0]];
    }
    
    province = [NSArray arrayWithArray:provinceTmp];
    
    NSString *index = [sortedArray objectAtIndex:0];
    NSString *selected = [province objectAtIndex:0];
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary: [[areaDic objectForKey:index]objectForKey:selected]];
    
    NSArray *cityArray = [dic allKeys];
    NSDictionary *cityDic = [NSDictionary dictionaryWithDictionary: [dic objectForKey: [cityArray objectAtIndex:0]]];
    city = [NSArray arrayWithArray:[cityDic allKeys]];
    
    selectedCity = [city objectAtIndex:0];
    district = [NSArray arrayWithArray:[cityDic objectForKey:selectedCity]];
    
    addressDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                   province,@"province",
                   city,@"city",
                   district,@"area",nil];
    
    selectedProvince = [province objectAtIndex:0];
    selectedArea = [district objectAtIndex:0];
    
    
    bgScrollView = [[LMContainsLMComboxScrollView alloc]initWithFrame:CGRectMake(0, 70, 320, 400)];
    bgScrollView.backgroundColor = [UIColor clearColor];
    bgScrollView.showsVerticalScrollIndicator = NO;
    bgScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:bgScrollView];
    
    [self setUpBgScrollView];
    
}
-(void)setUpBgScrollView
{
    UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 250, 320, 21)];
    titleLable.backgroundColor = [UIColor clearColor];
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.text = @"选择省市区";
    [bgScrollView addSubview:titleLable];
    
    NSArray *keys = [NSArray arrayWithObjects:@"province",@"city",@"area", nil];
    for(NSInteger i=0;i<3;i++)
    {
        LMComBoxView *comBox = [[LMComBoxView alloc]initWithFrame:CGRectMake(50+(63+3)*i, 55, 63, 24)];
        comBox.backgroundColor = [UIColor whiteColor];
        comBox.arrowImgName = @"down_dark0.png";
        NSMutableArray *itemsArray = [NSMutableArray arrayWithArray:[addressDict objectForKey:[keys objectAtIndex:i]]];
        comBox.titlesList = itemsArray;
        comBox.delegate = self;
        comBox.supView = bgScrollView;
        [comBox defaultSettings];
        comBox.tag = kDropDownListTag + i;
        [bgScrollView addSubview:comBox];
    }
}

- (void)viewDidUnload {
    [select release];
    select = nil;
    [self select:nil];
    [super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)selectClicked:(id)sender{
    NSArray * arr = [[NSArray alloc] init];
    arr = [NSArray arrayWithObjects:@"建设银行", @"招商银行", @"工商银行", @"中国银行", @"农业银行", @"交通银行", @"民生银行", @"华夏银行", @"兴业银行", @"浦东发展银行", @"广东发展银行" @"中信银行", @"光大银行", @"中国邮政储蓄", @"北京银行", @"天津银行", @"上海银行", @"平安银行", @"宁波银行", @"南京银行", nil];
    if(dropDown == nil) {
        CGFloat f = 200;
        dropDown = [[NIDropDown alloc]showDropDown:sender :&f :arr];
        dropDown.delegate = self;
    }
    else {
        [dropDown hideDropDown:sender];
        [self rel];
    }
}
- (void) niDropDownDelegateMethod: (NIDropDown *) sender {
    [self rel];
}

-(void)rel{
    [dropDown release];
    dropDown = nil;
}
#pragma mark -LMComBoxViewDelegate
//-(void)selectAtIndex:(int)index inCombox:(LMComBoxView *)_combox
//{
//    NSInteger tag = _combox.tag - kDropDownListTag;
//    switch (tag) {
//        case 0:
//        {
//            selectedProvince =  [[addressDict objectForKey:@"province"]objectAtIndex:index];
//            //字典操作
//            NSDictionary *tmp = [NSDictionary dictionaryWithDictionary: [areaDic objectForKey: [NSString stringWithFormat:@"%d", index]]];
//            NSDictionary *dic = [NSDictionary dictionaryWithDictionary: [tmp objectForKey: selectedProvince]];
//            NSArray *cityArray = [dic allKeys];
//            NSArray *sortedArray = [cityArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
//                
//                if ([obj1 integerValue] > [obj2 integerValue]) {
//                    return (NSComparisonResult)NSOrderedDescending;//递减
//                }
//                
//                if ([obj1 integerValue] < [obj2 integerValue]) {
//                    return (NSComparisonResult)NSOrderedAscending;//上升
//                }
//                return (NSComparisonResult)NSOrderedSame;
//            }];
//            
//            NSMutableArray *array = [[NSMutableArray alloc] init];
//            for (int i=0; i<[sortedArray count]; i++) {
//                NSString *index = [sortedArray objectAtIndex:i];
//                NSArray *temp = [[dic objectForKey: index] allKeys];
//                [array addObject: [temp objectAtIndex:0]];
//            }
//            city = [NSArray arrayWithArray:array];
//            
//            NSDictionary *cityDic = [dic objectForKey: [sortedArray objectAtIndex: 0]];
//            district = [NSArray arrayWithArray:[cityDic objectForKey:[city objectAtIndex:0]]];
//            //刷新市、区
//            addressDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
//                           province,@"province",
//                           city,@"city",
//                           district,@"area",nil];
//            LMComBoxView *cityCombox = (LMComBoxView *)[bgScrollView viewWithTag:tag + 1 + kDropDownListTag];
//            cityCombox.titlesList = [NSMutableArray arrayWithArray:[addressDict objectForKey:@"city"]];
//            [cityCombox reloadData];
//            LMComBoxView *areaCombox = (LMComBoxView *)[bgScrollView viewWithTag:tag + 2 + kDropDownListTag];
//            areaCombox.titlesList = [NSMutableArray arrayWithArray:[addressDict objectForKey:@"area"]];
//            [areaCombox reloadData];
//            
//            selectedCity = [city objectAtIndex:0];
//            selectedArea = [district objectAtIndex:0];
//            break;
//        }
//        case 1:
//        {
//            selectedCity = [[addressDict objectForKey:@"city"]objectAtIndex:index];
//            
//            NSString *provinceIndex = [NSString stringWithFormat: @"%d", [province indexOfObject: selectedProvince]];
//            NSDictionary *tmp = [NSDictionary dictionaryWithDictionary: [areaDic objectForKey: provinceIndex]];
//            NSDictionary *dic = [NSDictionary dictionaryWithDictionary: [tmp objectForKey: selectedProvince]];
//            NSArray *dicKeyArray = [dic allKeys];
//            NSArray *sortedArray = [dicKeyArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
//                
//                if ([obj1 integerValue] > [obj2 integerValue]) {
//                    return (NSComparisonResult)NSOrderedDescending;
//                }
//                
//                if ([obj1 integerValue] < [obj2 integerValue]) {
//                    return (NSComparisonResult)NSOrderedAscending;
//                }
//                return (NSComparisonResult)NSOrderedSame;
//            }];
//            
//            NSDictionary *cityDic = [NSDictionary dictionaryWithDictionary: [dic objectForKey: [sortedArray objectAtIndex: index]]];
//            NSArray *cityKeyArray = [cityDic allKeys];
//            district = [NSArray arrayWithArray:[cityDic objectForKey:[cityKeyArray objectAtIndex:0]]];
//            //刷新区
//            addressDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
//                           province,@"province",
//                           city,@"city",
//                           district,@"area",nil];
//            LMComBoxView *areaCombox = (LMComBoxView *)[bgScrollView viewWithTag:tag + 1 + kDropDownListTag];
//            areaCombox.titlesList = [NSMutableArray arrayWithArray:[addressDict objectForKey:@"area"]];
//            [areaCombox reloadData];
//            
//            selectedArea = [district objectAtIndex:0];
//            break;
//        }
//        case 2:
//        {
//            selectedArea = [[addressDict objectForKey:@"area"]objectAtIndex:index];
//            break;
//        }
//        default:
//            break;
//    }
//    NSLog(@"===%@===%@===%@",selectedProvince,selectedCity,selectedArea);
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
